<?php

namespace App\Http\Controllers\Admin;

use App\Models\Feedback;
use App\Models\RegUser;
use App\Models\RewardPunishSet;
use App\Models\RoleUser;
use App\Models\UserPerformance;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Company;
use App\Http\Requests\CompanyCreateRequest;
use App\Http\Requests\CompanyUpdateRequest;
use App\Models\AttendanceSet;
use Illuminate\Support\Facades\DB;
class InitController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
   
    public function index()
    {
        return view('admin.init.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    { 
        
        return view('admin.init.create')->with('corpid',$this->corpid);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CompanyCreateRequest $request)
    {
        if($this->company){
         return  $this->formatJson('error','请勿重复创建','','/admin/init/'.$this->company->id.'/edit');
        }

         if($this->user->getRole()==9 ){
             $data=$request->FillData();
             $data['corpid']=$this->user->old_corp_no;
             DB::transaction(function ()use(&$data) {
                 $data=Company::Create($data);
                 //试用一个月
                 $release = strtotime("$data->created_at +36 month");
                 $data->expire_at = date("Y-m-d H:m:s", $release);
                 $data->save();

                 //$date_param = date('md');
                 $year_param = date('Y');

                 $set_array = [
                     'corpid'=> $data->id,
                     'year_param'=>$year_param,
                 ];
                 AttendanceSet::create($set_array);

                 $user = User::where('old_corp_no',$data->corpid)->first();
                 $user->corpid = $data->id;
                 $user->save();

                 $reguser = RegUser::where('old_corp_no',$data->corpid)->first();
                 $reguser->corpid = $data->id;
                 $reguser->save();

                 $roleuser = RoleUser::where('old_corp_no',$data->corpid)->first();
                 $roleuser->corpid = $data->id;
                 $roleuser->save();

                 $uperf = UserPerformance::where('old_corp_no',$data->corpid)->first();
                 $uperf->corpid = $data->id;
                 $uperf->save();
                 
             });
             return  $this->formatJson('success','创建成功',$data,'/admin/init/'.$data->id.'/edit');
         }else{
             return  $this->formatJson('error','抱歉，您没有相关操作权限','','/admin/init');

         }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit()
    {
        //
        return view('admin.init.edit')
            ->with('corp_no',$this->user->old_corp_no)
            ->with('company',$this->company)
            ->with('corpid',$this->corpid);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CompanyUpdateRequest $request, $id)
    {
        //
        if($this->user->getRole()==9 ){
            $data=$request->FillData();
            $this->company->update($data);
            return  $this->formatJson('success','登录中',$this->company,'/sysadmin/feedback/init');
        }else{
            return  $this->formatJson('error','抱歉，您没有相关操作权限');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
